﻿
namespace Trentacular.Office.Server.Search.FacetedSearch.Providers
{
    /// <summary>
    /// A managed property facet builder implementation that handles field types of
    /// type <see cref="T:System.String[]" />.
    /// </summary>
    public class StringArrayManagedPropertyFacetBuilder
        : BaseManagedPropertyFacetBuilder<string[]>
    {
        /// <summary>
        /// Indicating whether the <see cref="ManagedPropertyFacetProvider"/> should
        /// sort the resulting facets by their keys.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if the <see cref="ManagedPropertyFacetProvider"/>
        /// should sort the resulting facets by their keys; otherwise, <c>false</c>.
        /// </value>
        public override bool SortByKey { get { return true; } }

        /// <summary>
        /// Gets the facet keys for a single search result's managed property value.
        /// </summary>
        /// <param name="value">The facet keys for a single search result's managed
        /// property value.</param>
        /// <returns>An array of keys.</returns>
        public override string[] GetKeys(string[] value)
        {
            return value;
        }

        /// <summary>
        /// Creates a facet for a given facet key and managed property value.
        /// </summary>
        /// <param name="key">The facet's key.</param>
        /// <param name="value">The managed property value.</param>
        /// <returns>The generated facet.</returns>
        public override Facet CreateFacet(string key, string[] value)
        {
            return new Facet(
                FacetedSearchContext,
                Configuration,
                key,
                key,
                string.Format("({0} = '{1}')",
                    ManagedPropertyName,
                    key.Replace("'", "''")
                    )
            );
        }
    }
}
